//
//  ViewController.m
//  GroceryDude
//
//  Created by 陈记权 on 11/9/15.
//  Copyright © 2015 陈记权. All rights reserved.
//

#import "ViewController.h"
#import "LTCoreDataHelper.h"
#import "Item+CoreDataProperties.h"
#import "Measurement+CoreDataProperties.h"
#import "Amount+CoreDataProperties.h"
#import "Unit+CoreDataProperties.h"

@interface ViewController ()

@property (nonatomic, strong, readonly) LTCoreDataHelper *coreDataHelper;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(saveCoreDataContext)
                                                 name:UIApplicationDidEnterBackgroundNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(saveCoreDataContext)
                                                 name:UIApplicationWillTerminateNotification
                                               object:nil];
    [self cdh];
    
    //[self insertMeasurement];
    
    //[self fetchAmount];
    
    [self fetchUnit];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (LTCoreDataHelper *)cdh
{
    if (!_coreDataHelper) {
        _coreDataHelper = [LTCoreDataHelper new];
        [_coreDataHelper setupCoreData];
    }
    return _coreDataHelper;
}

- (void)saveCoreDataContext
{
    [[self cdh]saveContext];
}


- (void)insertMeasurement
{
    for (int i = 0; i < 50000; i ++) {
        Measurement *newMeasurement = [NSEntityDescription insertNewObjectForEntityForName:@"Measurement"
                                                                    inManagedObjectContext:_coreDataHelper.context];
        
        newMeasurement.abc = [NSString stringWithFormat:@"---->>> Loats Of Test Data x%i", i];
        NSLog(@"Inserted %@", newMeasurement.abc);
    }
    [_coreDataHelper saveContext];
}

- (void)firstDemo
{
    NSArray *newItems = [NSArray arrayWithObjects:@"Apples", @"Milk", @"Bread", @"Cheese", @"Sausages", @"Butter", nil];
    
    for (NSString *newItemName in newItems) {
        Item *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:_coreDataHelper.context];
        newItem.name = newItemName;
    }
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"name != %@", @"Cheese"];
    [request setPredicate:filter];
    
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    [request setSortDescriptors:@[sort]];
    
    NSArray *fetchedObjects = [_coreDataHelper.context executeFetchRequest:request error:nil];
    for (Item * item in fetchedObjects) {
        NSLog(@"%@", item.name);
    }
}

- (void)fetchAmount
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Amount"];
    [request setFetchLimit:50];
    
    NSError *error;
    NSArray *fetchedObjects = [_coreDataHelper.context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"%@", error);
    } else {
        for (Amount *amount in fetchedObjects) {
            NSLog(@"%@", amount.xyz);
        }
    }
}

- (void)fetchUnit
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Unit"];
    [request setFetchLimit:50];
    
    NSError *error;
    NSArray *fetchedObjects = [_coreDataHelper.context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"%@", error);
    } else {
        for (Unit *unit in fetchedObjects) {
            NSLog(@"%@", unit.name);
        }
    }
}

@end
